package com.lingyi.algorithm.lab2;

import cn.hutool.core.collection.CollUtil;

import java.util.ArrayList;

/**
 * 有序链表
 * @author chenweilong
 * @email 1433471850@qq.com
 * @date 2020-08-19 17:33
 */
public class OrderedLink {




    public static void main(String[] args) {

        ArrayList<Integer> ints = CollUtil.newArrayList(10, 8, 4, 20, 11, 7, 4);

        Node<Integer> head = new Node();

        for (Integer anInt : ints) {
            Node<Integer> p = head;
            while (true) {
                Node<Integer> e;
                Node newNode = new Node();
                newNode.setT(anInt);
                if ((e = p.getNext()) == null) {
                    p.setNext(newNode);
                    break;
                    //放到当前元素的前头
                }else if (e.getT() > anInt) {
                    newNode.setNext(e);
                    p.setNext(newNode);
                    break;
                }
                p = e;
            }
        }

        while (head.getNext() != null) {
            head = head.getNext();
            System.out.println(head.getT());
        }

    }

}
